package com.yxhl.zoume.core.user.ui.fragment.mywallet;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yxhl.zoume.BR;
import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.user.presenter.mywallet.TradeRecordDetailPresenter;
import com.yxhl.zoume.core.user.ui.activity.MyWalletDetailActivity;
import com.yxhl.zoume.core.user.view.TradeRecordDetailView;
import com.yxhl.zoume.data.http.model.mywallet.TradeRecordDetailBean;
import com.yxhl.zoume.di.component.usercenter.DaggerMyWalletComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.PromptUtils;

import java.util.List;
import java.util.ListIterator;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/11/3 .
 */

public class ExpenseDetailFragment extends BaseDataBindingFragment implements TradeRecordDetailView {


    private String mOrderId;
    @Inject
    TradeRecordDetailPresenter mTradeRecordDetailPresenter;

    private ViewDataBinding mBinding;
    private boolean mHandledPress = false;
    MaterialDialog dialogForWaitingDetail = null;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        parseArguments();
        mTradeRecordDetailPresenter.attachView(this);
        mTradeRecordDetailPresenter.queryTradeRecordDetail(mOrderId);
        showWaitingDialog(true);

    }

    @Override
    public void onResume() {
        super.onResume();
        mTradeRecordDetailPresenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mTradeRecordDetailPresenter.onPause();
    }

//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        MyWalletActivity activity = (MyWalletActivity) mActivity;
//        activity.showTriangle();
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mTradeRecordDetailPresenter.onDestroy();
    }

    private void parseArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mOrderId = arguments.getString(UIConstants.MyWallet.ARGUMENT_TRADE_TYPE, "");
        }
    }

    public static ExpenseDetailFragment newInstance(String orderSerial) {
        Bundle args = new Bundle();
        args.putSerializable(UIConstants.MyWallet.ARGUMENT_TRADE_TYPE, orderSerial);
        ExpenseDetailFragment fragment = new ExpenseDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initializeInjector() {
        DaggerMyWalletComponent
                .builder()
                .activityModule(new ActivityModule(mActivity))
                .appComponent(getAppComponent())
                .build()
                .inject(this);

    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_inex_expenses_detail, container, false);
        return mBinding.getRoot();
    }

    /**
     * 测试
     *
     * @return
     */
    public boolean onBackPressed() {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        fragmentManager.removeOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                ExpenseDetailFragment.this.onDestroy();
            }
        });
        List<Fragment> fragments = fragmentManager.getFragments();
        ListIterator<Fragment> iterator = fragments.listIterator();
        while (iterator.hasNext()) {
            Fragment fragment = iterator.next();
            if (fragment == this) {
                iterator.remove();
            }
        }
        if (!mHandledPress) {
            mHandledPress = true;
            return true;
        }
        return false;
    }

    @Override
    protected void setupToolBar() {
        ((MyWalletDetailActivity) mActivity).setToolbarTitle("消费详情");
    }

    @Override
    public void renderTradeRecordDetailOnSuccess(TradeRecordDetailBean tradeRecordDetail) {
        showWaitingDialog(false);
        mBinding.setVariable(BR.TradeRecordDetail, tradeRecordDetail);
    }

    @Override
    public void renderTradeRecordDetailOnFailure(String errorMessage) {
        showWaitingDialog(false);
        PromptUtils.showShort(mActivity, errorMessage);
    }

    @Override
    public void renderTradeRecordDetailOnError() {
        showWaitingDialog(false);

    }

    public void showWaitingDialog(boolean isWaiting) {
        if (isWaiting) {
            dialogForWaitingDetail = new MaterialDialog.Builder(mActivity)
                    .content(R.string.dialog_waiting_processing)
                    .progress(true, 0)
                    .canceledOnTouchOutside(false)
                    .show();
        } else {
            if (null != dialogForWaitingDetail) {
                dialogForWaitingDetail.dismiss();
            }
        }

    }
}
